﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Linq;
using IntoNails.DataAccess;
using IntoNails.Infrastructure;
using IntoNails.Infrastructure.InteractionRequests;
using IntoNails.Models;
using IntoNails.Modules.UserManagement.EventAggregators;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.ViewModel;

namespace IntoNails.Modules.UserManagement.ViewModels
{
	[Export(typeof(IUserWorkspaceViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
	public partial class UserWorkspaceViewModel : NotificationObject, IUserWorkspaceViewModel
	{
		#region Fields

	    private readonly IEventAggregator _eventAggregator;

        private readonly UserRepository _userRepository;

		private ObservableCollection<User> _userCollection; 
		private User _selectedUser;

		#endregion
		
		#region Constructors

        [ImportingConstructor]
		public UserWorkspaceViewModel(IEventAggregator eventAggregator)
        {
            this._eventAggregator = eventAggregator;

            this._eventAggregator.GetEvent<CreateNewUserEventAggregator>().Subscribe(OnCreateNewUserEventSubscribed, ThreadOption.PublisherThread);
            this._eventAggregator.GetEvent<EditUserEventAggregator>().Subscribe(OnEditUserEventSubscribed, ThreadOption.UIThread);
            this._eventAggregator.GetEvent<DeleteUserEventAggregator>().Subscribe(OnDeleteUserEventSubscribed, ThreadOption.UIThread);
            
            this.CreateNewUserNotification = new GenericInteractionRequest<User>();
            this.ShowMessageBox = new GenericInteractionRequest<MessageBoxContext>();

            this._userRepository = new UserRepository(GlobalInfoCaches.StartUpPath + @"\User.dat");
            this._userRepository.Load();

            this.PopulateUserList();
        }
		
		#endregion

        #region Interaction Requests

        public GenericInteractionRequest<User> CreateNewUserNotification { get; private set; }

        public GenericInteractionRequest<MessageBoxContext> ShowMessageBox { get; private set; }

        #endregion

        #region Properties

        public ObservableCollection<User> UserCollection
        {
            get { return this._userCollection; }
            set
            {
                this._userCollection = value;
                base.RaisePropertyChanged(() => UserCollection);
            }
        }

        public User SelectedUser
        {
            get { return this._selectedUser; }
            set
            {
                this._selectedUser = value;
                base.RaisePropertyChanged(() => SelectedUser);
            }
        }

        #endregion

        #region Private Methods

        private void PopulateUserList()
        {
            this.UserCollection = new ObservableCollection<User>(this._userRepository.GetRawItemList());

        }

        #endregion
    }
}
